package mn.buya;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import mn.buya.popup.ActionItem;
import mn.buya.popup.QuickAction;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Үгийн хүснэгт
 * 
 * @author buyanzaya
 * 
 */
public class WordTableGenerator extends Main {

	DBAdapter db = new DBAdapter();
	List<String> lRawWords = new ArrayList<String>();

	private QuickAction quickAction;
	private Context cThis;

	public WordTableGenerator(Context ctx) {
		super();
		cThis = ctx;
		lRawWords = db.getRawWords();
		Collections.shuffle(lRawWords);
	}

	private Button createButton(final Context ctx, int iIndex) {

		LinearLayout.LayoutParams widgetParams = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, 60, 1.0F);

		final Button b = new Button(ctx);
		b.setId(iIndex);
		b.setText("*");
		b.setTextColor(Color.RED);
		b.setLayoutParams(widgetParams);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
//				try {
					quickAction = new QuickAction(cThis);
					ActionItem actionItem = new ActionItem();
					actionItem.setTitle(lRawWords.get(b.getId()));
					quickAction.addActionItem(actionItem);
					showDialogParent(ctx, lRawWords.get(b.getId()), arg0);
					checkParent(lRawWords.get(b.getId()));
//				} catch (Exception e) {
//					System.out.println("Алдаа : " + e.toString());
//					showDialogParent(ctx, "", arg0);
//				}

			}
		});

		return b;
	}
	
	private void checkParent(String sValue) {
		super.check(sValue, cThis);
	}

	private void showDialogParent(Context ctx, String sWord, View view) {

		quickAction.show(view);
	}

	public void generateWordTable(Context ctx) {
		/*
		 * Андройд дэлгэцийн хэмжээнүүд
		 */
		LinearLayout llWordTable1 = (LinearLayout) ((Activity) ctx)
				.findViewById(R.id.llwordtable1);
		LinearLayout llWordTable2 = (LinearLayout) ((Activity) ctx)
				.findViewById(R.id.llwordtable2);
		LinearLayout llWordTable3 = (LinearLayout) ((Activity) ctx)
				.findViewById(R.id.llwordtable3);
		LinearLayout llWordTable4 = (LinearLayout) ((Activity) ctx)
				.findViewById(R.id.llwordtable4);
		LinearLayout llWordTable5 = (LinearLayout) ((Activity) ctx)
				.findViewById(R.id.llwordtable5);

		int iTmpIndex = 0;

		for (int i = 0; i < 7; i++) {
			llWordTable1.addView(createButton(ctx, iTmpIndex));
			iTmpIndex++;
			llWordTable2.addView(createButton(ctx, iTmpIndex));
			iTmpIndex++;
			llWordTable3.addView(createButton(ctx, iTmpIndex));
			iTmpIndex++;
			llWordTable4.addView(createButton(ctx, iTmpIndex));
			iTmpIndex++;
			llWordTable5.addView(createButton(ctx, iTmpIndex));
			iTmpIndex++;
		}
	}

}